﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EIA.SLClient.Utility
{
    public partial class DataPagerControl : UserControl
    {
        public DataPagerControl()
        {
            // 为初始化变量所必需
            InitializeComponent();
        }

        #region PageSize (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int), typeof(DataPagerControl),
            new PropertyMetadata(1, new PropertyChangedCallback(OnPageSizeChanged)));

        private static void OnPageSizeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            DataPagerControl source = (DataPagerControl)sender;

            // 处理逻辑
            int newValue = (int)args.NewValue;
            source.txt_page_size.Text = newValue.ToString();
            source.txt_page_count.Text = source.CalcPageCount().ToString();
        }

        #endregion

        #region TotalCount (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public int TotalCount
        {
            get { return (int)GetValue(TotalCountProperty); }
            set { SetValue(TotalCountProperty, value); }
        }
        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register("TotalCount", typeof(int), typeof(DataPagerControl),
            new PropertyMetadata(0, new PropertyChangedCallback(OnTotalCountChanged)));

        private static void OnTotalCountChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            DataPagerControl source = (DataPagerControl)sender;

            // 处理逻辑
            int newValue = (int)args.NewValue;

            source.txt_total_count.Text = newValue.ToString();
            source.txt_page_count.Text = source.CalcPageCount().ToString();
        }

        #endregion

        #region PageIndex (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public int PageIndex
        {
            get { return (int)GetValue(PageIndexProperty); }
            set { SetValue(PageIndexProperty, value); }
        }
        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register("PageIndex", typeof(int), typeof(DataPagerControl),
            new PropertyMetadata(0, new PropertyChangedCallback(OnPageIndexChanged)));

        private static void OnPageIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            DataPagerControl source = (DataPagerControl)sender;

            // 处理逻辑
            int newValue = (int)args.NewValue;
            source.txt_page_index.Text = newValue.ToString();
        }

        #endregion

        private int CalcPageCount()
        {
            if (PageSize < 1) return 0;
            int pagecount = (int)Math.Ceiling(((double)TotalCount) / PageSize);
            return pagecount;
        }

        private void btn_page_first_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (CalcPageCount() == 0) return;
            PageIndex = 1;
        }

        private void btn_page_last_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (CalcPageCount() == 0) return;
            PageIndex = CalcPageCount();
        }

        private void btn_page_pre_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (CalcPageCount() == 0) return;
            if (PageIndex > 1)
            {
                PageIndex--;
            }
        }

        private void btn_page_nxt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (CalcPageCount() == 0) return;
            if (PageIndex < CalcPageCount())
            {
                PageIndex++;
            }
        }

        private void SetPageButtons()
        {
            //int pagecount = CalcPageCount();
            //if (pagecount == 0)
            //{
            //    this.btn_page_0.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_1.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_2.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_3.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_4.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_5.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_6.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_7.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_8.Visibility = System.Windows.Visibility.Collapsed;
            //    this.btn_page_9.Visibility = System.Windows.Visibility.Collapsed;
            //}
            //else if (pagecount <= 10)
            //{
            //    for (int i = 0; i < this.cnt_pages.Children.Count; i++)
            //    {
            //        (this.cnt_pages.Children[i] as Button).Visibility
            //    }
            //}
        }
    }
}